﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vision.Camera.Base
{
    public static class CameraManager
    {
        static Dictionary<string, ICamera> CameraFactorys { get; set; } = new Dictionary<string, ICamera>(); //相机类别列表

        public static Dictionary<string, ICamera> Cameras { get; set; } = new Dictionary<string, ICamera>();//相机列表
        
        //添加相机品牌
        public static void AddBrand(string brand,ICamera camera)
        {
            CameraFactorys.Add(brand, camera);
        }

        /// <summary>
        /// 扫描相机
        /// </summary>
        public static void Scan()
        {
            foreach(var camera in CameraFactorys)
            {
                List<ICamera> cameras = camera.Value.Scan(camera.Key);
                foreach(ICamera item in cameras)
                {
                    Cameras.Add(item.Name, item);
                    item.OpenCam();
                }
            }
            //test
            //Cameras.Add("t1", null);
            //Cameras.Add("t2", null);
        }
        
        public static string[] GetCameraNames()
        {
            return Cameras.Keys.ToArray();
        }
        public static bool ReName(string oldName,string newName)
        {
            if(!string.IsNullOrEmpty(oldName) && !string.IsNullOrEmpty(newName) && oldName != newName)
            {
                ICamera camera;
                if(Cameras.TryGetValue(oldName, out camera) && !Cameras.ContainsKey(newName))
                {
                    Cameras.Remove(oldName);
                    Cameras.Add(newName, camera);
                    return true;
                }
            }
            return false;

        }
        /// <summary>
        /// 获取相机
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ICamera GetCamera(string name)
        {
            ICamera camera;
            bool rc = Cameras.TryGetValue(name,out camera);
            return camera;
        }

        //关闭相机
        public static void CloseAllCamera()
        {
            foreach(var item in Cameras)
            {
                item.Value?.CloseCam();
            }
        }

    }
}
